﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
using System.ComponentModel;

namespace EIA.SLClient.ProjectManage.Progress
{
    public class ProjectProgressViewModel : SimpleMvvmToolkit.ViewModelDetailBase<ProjectProgressViewModel, EIA.PM.Model.LProjectProgress>
    {
        public ProjectProgressViewModel()
        {

        }


        private void Busy(string msg = "等待...")
        {
            IsBusy = true;
            SendMessage<bool>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>(msg, true));
        }

        private void Free()
        {
            IsBusy = false;
            SendMessage<bool>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>("", false));
        }

        #region notifyprop

        protected bool _IsBusy;
        /// <summary>
        /// 
        /// </summary>
        public bool IsBusy
        {
            get { return _IsBusy; }
            set
            {
                _IsBusy = value;
                NotifyPropertyChanged(m => m.IsBusy);
            }
        }

        protected bool _IsNew;
        /// <summary>
        /// 是否为添加操作
        /// </summary>
        public bool IsNew
        {
            get { return _IsNew; }
            set
            {
                _IsNew = value;
                NotifyPropertyChanged(m => m.IsNew);
            }
        }

        protected System.Collections.ObjectModel.ObservableCollection<int> _ProgressCollection;
        /// <summary>
        /// 进度列表
        /// </summary>
        public System.Collections.ObjectModel.ObservableCollection<int> ProgressCollection
        {
            get { return _ProgressCollection; }
            set
            {
                _ProgressCollection = value;
                NotifyPropertyChanged(m => m.ProgressCollection);
            }
        }

        protected int _Progress;
        /// <summary>
        /// 当前UI选择进度编号
        /// </summary>
        public int Progress
        {
            get { return _Progress; }
            set
            {
                _Progress = value;
                NotifyPropertyChanged(m => m.Progress);
                LoadModel();
            }
        }

        protected EIA.PM.Model.TProjectBasic _ProjectBasic;
        /// <summary>
        /// 注释
        /// </summary>
        public EIA.PM.Model.TProjectBasic ProjectBasic
        {
            get { return _ProjectBasic; }
            set
            {
                _ProjectBasic = value;
                NotifyPropertyChanged(m => m.ProjectBasic);
            }
        }
        #endregion

        #region command

        #region NewCommand

        private SimpleMvvmToolkit.DelegateCommand _NewCommand = null;
        public SimpleMvvmToolkit.DelegateCommand NewCommand
        {
            get
            {
                if (_NewCommand == null)
                {
                    _NewCommand = new SimpleMvvmToolkit.DelegateCommand(New, CanExcuteNew);
                } return _NewCommand;
            }
            set { _NewCommand = value; NotifyPropertyChanged(m => m.NewCommand); }
        }

        public void New()
        {
            this.Model = new PM.Model.LProjectProgress() { PrjId = ProjectBasic.PrjId, Progress = Progress, PrgDate = DateTime.Now };
            IsNew = true;
            BeginEdit();
        }

        public bool CanExcuteNew()
        {
            return true;
        }

        #endregion

        #region SaveCommand

        private SimpleMvvmToolkit.DelegateCommand _SaveCommand = null;
        public SimpleMvvmToolkit.DelegateCommand SaveCommand
        {
            get
            {
                if (_SaveCommand == null)
                {
                    _SaveCommand = new SimpleMvvmToolkit.DelegateCommand(Save, CanExcuteSave);
                } return _SaveCommand;
            }
            set { _SaveCommand = value; NotifyPropertyChanged(m => m.SaveCommand); }
        }

        public void Save()
        {
            EIA.PM.BLL.ClientProxy.LProjectProgressProxy proxy = new PM.BLL.ClientProxy.LProjectProgressProxy();
            Busy();
            if (IsNew)
            {
                proxy.Insert(Model, (k) =>
                {
                    Free();
                    Model.PrgId = k;
                    EndEdit();
                    IsNew = false;
                    UpdateProjectBasic();
                }, (ex) =>
                {
                    Free();
                    System.Windows.MessageBox.Show("添加项目进度记录时发生错误！", "提示", System.Windows.MessageBoxButton.OK);
                });
            }
            else
            {
                proxy.Update(Model, (r) =>
                {
                    Free();
                    if (r)
                    {
                        EndEdit();
                        UpdateProjectBasic();
                    }
                    else
                    {
                        System.Windows.MessageBox.Show("修改失败！", "提示", System.Windows.MessageBoxButton.OK);
                    }
                }, (ex) =>
                {
                    Free();
                    System.Windows.MessageBox.Show("修改时发生错误！", "提示", System.Windows.MessageBoxButton.OK);
                });
            }
        }

        private void UpdateProjectBasic()
        {
            if (!ProjectBasic.Progress.HasValue || ProjectBasic.Progress.Value < Progress)
            {
                ProjectBasic.Progress = Progress;
                Busy();
                EIA.PM.BLL.ClientProxy.TProjectBasicProxy basicProxy = new PM.BLL.ClientProxy.TProjectBasicProxy();
                basicProxy.Update(ProjectBasic, (r) =>
                {
                    Free();
                    if (!r)
                    {
                        System.Windows.MessageBox.Show("更新项目进度失败！", "提示", System.Windows.MessageBoxButton.OK);
                    }
                }, (ex) =>
                {
                    Free();
                    System.Windows.MessageBox.Show("更新项目进度时发生错误！", "提示", System.Windows.MessageBoxButton.OK);
                });
            }
        }

        public bool CanExcuteSave()
        {
            return true;
        }

        #endregion

        #region CancelCommand

        private SimpleMvvmToolkit.DelegateCommand _CancelCommand = null;
        public SimpleMvvmToolkit.DelegateCommand CancelCommand
        {
            get
            {
                if (_CancelCommand == null)
                {
                    _CancelCommand = new SimpleMvvmToolkit.DelegateCommand(Cancel, CanExcuteCancel);
                } return _CancelCommand;
            }
            set { _CancelCommand = value; NotifyPropertyChanged(m => m.CancelCommand); }
        }

        public void Cancel()
        {
            this.CancelEdit();
            //if (IsNew)
            //{
            //    this.Model = null;
            //    IsNew = false;
            //}
        }

        public bool CanExcuteCancel()
        {
            return true;
        }

        #endregion


        #region EditCommand

        private SimpleMvvmToolkit.DelegateCommand _EditCommand = null;
        public SimpleMvvmToolkit.DelegateCommand EditCommand
        {
            get
            {
                if (_EditCommand == null)
                {
                    _EditCommand = new SimpleMvvmToolkit.DelegateCommand(Edit, CanExcuteEdit);
                } return _EditCommand;
            }
            set { _EditCommand = value; NotifyPropertyChanged(m => m.EditCommand); }
        }

        public void Edit()
        {
            this.BeginEdit();
        }

        public bool CanExcuteEdit()
        {
            return true;
        }

        #endregion

        #endregion


        public void OnLoad()
        {
            this.SendMessage<object, EIA.PM.Model.TProjectBasic>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.PROJECT_CURRENT, new SimpleMvvmToolkit.NotificationEventArgs<object, PM.Model.TProjectBasic>("", null, (p) =>
            {
                ProjectBasic = p;
            }));
            var progressCollection = new System.Collections.ObjectModel.ObservableCollection<int>();
            foreach (var di in ListsResource.Instance.XMJDCollection)
            {
                progressCollection.Add(di.DicIid);
            }
            ProgressCollection = progressCollection;
            Progress = ProjectBasic != null && ProjectBasic.Progress.HasValue ? ProjectBasic.Progress.Value : ProgressCollection[0];
        }

        private void LoadModel()
        {
            if (ProjectBasic == null) return;
            EIA.PM.BLL.ClientProxy.LProjectProgressProxy proxy = new PM.BLL.ClientProxy.LProjectProgressProxy();
            Dictionary<string, object> con = new Dictionary<string, object>();
            con[EIA.PM.Model.LProjectProgress.PRJ_ID] = ProjectBasic.PrjId;
            con[EIA.PM.Model.LProjectProgress.PROGRESS] = Progress;
            Busy();
            if (IsEditing) CancelEdit();
            Model = null;
            proxy.List(con, 1, 1, (lst) =>
            {
                Free();
                if (lst == null || lst.Count == 0)
                {
                    Model = null;
                    New();
                }
                else
                {
                    Model = lst[0];
                }
            }, (ex) =>
            {
                Free();
                System.Windows.MessageBox.Show("获取进度记录发生错误", "提示", System.Windows.MessageBoxButton.OK);
            });
        }
    }
}